import csv

import requests
import time
import asyncio
import aiohttp
from fake_useragent import UserAgent
import json

url = 'https://www.governbond.org.cn:4443/api/loadBondData.action?timeStamp=1744284092347&dataType=ZQFXLISTBYAD&adList=&adCode=87&zqlx=&year=&fxfs=&qxr=&fxqx=&zqCode=&zqName=&page=1&pageSize=10'
ua = UserAgent()
paramss = []
for i in range(1, 1642):
    params = {
        'dataType': 'ZQFXLISTBYAD',
        'adCode': '87',
        'page': f'{i}',
        'pageSize': '10',
    }
    paramss.append(params)


async def fetch(session):
    while paramss:
        params = paramss.pop()
        await asyncio.sleep(1)
        async with session.get(url, headers={'User-Agent': ua.random}, params=params) as res:
            res1 = await res.text()
            res1 = json.loads(res1)
            data = []
            for item in res1['data']:
                print(f'正在爬取第{item['ZQ_NAME']}页')
                ZQ_NAME = item['ZQ_NAME']
                ZQ_CODE = item['ZQ_CODE']
                AD_NAME = item['AD_NAME']
                ZQ_FXTIME = item['ZQ_FXTIME']
                ZQQX_NAME = item['ZQQX_NAME'].strip()
                LXFS_ID = item['LXFS_ID']
                LL = item['LL']
                FXFS = item['FXFS']
                QX_DATE = item['QX_DATE']
                data.append([ZQ_NAME, ZQ_CODE, AD_NAME, ZQ_FXTIME, ZQQX_NAME, LXFS_ID, LL, FXFS, QX_DATE])
                time.sleep(1)

                # print(ZQ_NAME,ZQ_CODE,AD_NAME,ZQ_FXTIME,ZQQX_NAME,LXFS_ID,LL,FXFS,QX_DATE)
                with open('地方债.csv', 'w', encoding='utf-8', newline='') as f:
                    writer = csv.writer(f)
                    writer.writerow(
                        ['债卷名', '债卷代码', '地区', '发行时间', '发行期限', '利率', '付息方式', '到期日期'])
                    writer.writerows(data)


async def task():
    async with aiohttp.ClientSession() as session:
        return await fetch(session)


async def main():
    await asyncio.gather(*[asyncio.create_task(task()) for i in range(1, 10)])


asyncio.run(main())
